package hibernate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;




import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pojo.Deals;
import pojo.Orders;
import pojo.Orderstatus;
import pojo.Productcatalog;
import dao.OrdersDAO;
@Repository("OrdersDAO")
public class HibernateOrdersDAO implements OrdersDAO {
	private SessionFactory sessionFactory;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Transactional
	public boolean addOrders(Orders order) {
		Session session = sessionFactory.getCurrentSession();
        try
        {
            session.save(order);
            return true;
        }
        catch(HibernateException e)
        {
            System.err.print(e); 
        }
        return false;
	}

	@Transactional
	public List<Orders> getListOrderByEmail(String email, int filterorder) {
		Session session = sessionFactory.getCurrentSession();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar  c = Calendar.getInstance();
		
		String hql =String.format("from Orders o where o.user.email ='%s' ",email);
        if(filterorder ==0){
        	c.add(Calendar.DAY_OF_YEAR, -7);
    		Date thisweek =  c.getTime();
    		String formattedweek = formatter.format(thisweek);
        	hql+=" and o.orderdate > '" + formattedweek +"'";
        }
        	
        if(filterorder ==1){
        	c.add(Calendar.DAY_OF_YEAR, -30);
    		Date thismonth =  c.getTime();
    		String formattedmonth = formatter.format(thismonth);
        	hql+=" and o.orderdate >'" + formattedmonth +"'";
        }
        	
        if(filterorder ==2){
    		c.add(Calendar.DAY_OF_YEAR, -365);
    		Date thisyear =  c.getTime();
    		String formattedyear = formatter.format(thisyear);
        	hql+=" and o.orderdate >'" + formattedyear +"'";
        }
        	
        Query query = session.createQuery(hql);
        List<Orders> listOrders = query.list();
        return listOrders;
	}
	
	@Transactional
	public  List<Orders> getDataSource(String email) {
		List<Orders> listOrders = new ArrayList<Orders>();
		Session session = sessionFactory.getCurrentSession();
		String hql = "from Orders o where o.user.email = '"+ email +"'";
//		String hql = "SELECT orders.orderid as orderid, orders.totalmoney as totalmoney, "
//				+ "orders.orderstatus.orderstatusname AS orderstatus_orderstatusname, "
//				+ "orders.orderdate as orderdate, "	
//						+"orders.user.email AS user_email "
//						+ "FROM	Orders orders";
		Query query = session.createQuery(hql);
        listOrders = query.list();
		// Return the wrapped collection
		return listOrders;
	}

	@Transactional
	public Orders getOrders(String orderid) {
		Session session = sessionFactory.getCurrentSession();
        if(orderid.equals("-1"))
        	return null;
        String hql = String.format("from Orders o where o.orderid= %s", orderid);
        Query query = session.createQuery(hql);

        return (Orders) query.uniqueResult(); 
	}

}
